//
//  FlowControlViewController.m
//  NaRiLSVMT
//
//  Created by jmsre on 14-8-18.
//  Copyright (c) 2014年 helloyouth.org. All rights reserved.
//

#import "FlowControlViewController.h"
#import "FlowControlViewController+Request.h"
#import "MBProgressController.h"



static  NSInteger   pageLeftCounts = 0;//往左还有几屏
static  NSInteger   pageRightCounts = 0;//往右还有几屏
static  NSInteger   pageAllLeft = 0;//往左共有几屏.


static  NSMutableArray  *flowButtonArr = nil;//流程控制主界面下面的所有按钮集
static  NSInteger   buttonIndex = 0;//流程控制主界面下面的所有按钮集中的按钮索引
NSInteger   lastSelectedButtonID = 0;//已经选择了的按钮ID

static  NSMutableArray  *flowThumbArr = nil;//流程主界面下的缩略图数组


//extern  NSInteger   screenNo;//每个节点中的第几个屏幕
extern  dispatch_source_t timer_playFlowScreen;//播放定时器
extern BOOL    isPlayingFlag;//是否正在单次播放或者循环播放标识


NSMutableDictionary *playButtonDic = nil;//在流程控制界面，被点击播放的按钮

NSInteger   lastFlowNumber = -1;//前一个处理的流程No.或者当前

BOOL    currentFlowIsPlaying = NO;//是否是当前流程在播放

BOOL    isNotNeedTurnNode = NO;//如果是本流程在播放，则需要切节点，否则不需要切节点


UIImageView *topImageViewInFlow =  nil;//流程控制主页面，背景图片


extern NSInteger   isCanJumptoDetailUI;//禁止快速点击流程进入节点界面
NSInteger   isCanActionOtherButton = 0;//禁止快速点击播放按钮

extern  NSString    *intervalPlayTime;//播放间隔时间

@interface FlowControlViewController ()<UITextFieldDelegate>
{
    UIImageView *flowPlayShadowImageV;
}

@end

@implementation FlowControlViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil Date:(NSArray *)data
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.receiveData = [NSMutableArray arrayWithArray:data];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    self.navigationController.navigationBar.backItem.hidesBackButton = YES;
//    [self.navigationItem setHidesBackButton:YES];
//    self.navigationController.navigationBar.hidden = YES;
//    [self.view addSubview:self.navigationView];
    
    self.topViewInFlow.layer.borderWidth = 0.5;
    self.topViewInFlow.layer.cornerRadius = 10.0;
    self.topViewInFlow.layer.borderColor = [UIColor grayColor].CGColor;
    self.topViewInFlow.clipsToBounds = YES;
    
    if(topImageViewInFlow != nil){
    [topImageViewInFlow removeFromSuperview];
    }
    topImageViewInFlow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"flowControl.jpg"]];
    topImageViewInFlow.frame = CGRectMake(0, 0, Screen_Width-20, 430);
    [self.topViewInFlow addSubview:topImageViewInFlow];
    /***************初始化一些变量*****************/
    pageLeftCounts = 0;
    pageRightCounts = 0;
    pageAllLeft = 0;
    buttonIndex = 0;
    lastSelectedButtonID = 0;
    
    self.textFieldProme = [[UIView alloc] init];
    self.textFieldProme.frame = self.flowNoteIntervalTime.frame;
    self.textFieldProme.backgroundColor = [UIColor grayColor];
    self.textFieldProme.alpha = 0.8;
    setViewCorner(self.textFieldProme,4);
    [self.bottomSmallView addSubview:self.textFieldProme];
    self.textFieldProme.hidden = YES;
    
    self.flowNoteIntervalTime.delegate = self;
    /********************************************/
    //init thumbView:
    flowButtonArr = nil;
    flowThumbArr = nil;
    flowButtonArr = [NSMutableArray array];
    flowThumbArr = [NSMutableArray array];
    
    self.flowCounts.text = [NSString stringWithFormat:@"%lu",(unsigned long)[self.receiveData count]];
    pageLeftCounts = (self.flowCounts.text.integerValue-1)/4;
    pageAllLeft = pageLeftCounts;
    
    self.allThumbScrollView.contentSize = CGSizeMake((944/4)*[self.receiveData count], self.allThumbScrollView.contentSize.height);
    
    self.allThumbScrollView.layer.borderWidth = 3.0;
    self.allThumbScrollView.layer.borderColor = ColorRGB(133,183,184).CGColor;
    setViewCorner(self.allThumbScrollView,6);
    
    
    [self initThumbViewInBottom:self.receiveData];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    if ([intervalPlayTime intValue] < 5) {
        self.flowNoteIntervalTime.text = @"5";
    }
    else{
        self.flowNoteIntervalTime.text = intervalPlayTime;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//init ThumbView:
-(void)initThumbViewInBottom:(NSMutableArray *)thumViewArr
{
    
    //添加流程控制的数据
    for(NSInteger i=0;i<[thumViewArr count];i++)
    {
        ThumbView *thumbnailsView = LoadFromNib(@"ThumbView");
        [flowThumbArr addObject:thumbnailsView];
        //负值
        thumbnailsView.flowImageName.text = [[thumViewArr objectAtIndex:i] objectForKey:@"name"];
        thumbnailsView.flowContent.text = [[thumViewArr objectAtIndex:i] objectForKey:@"flowInfo"];
        [thumbnailsView.flowButton setTag:100+(buttonIndex++)];
        [thumbnailsView.oncePlayButton setTag:100+(buttonIndex++)];
        [thumbnailsView.cyclesPlayButton setTag:100+(buttonIndex++)];
        thumbnailsView.delegateThumbView = self;
        
        // 添加所有流程的 播放  点击 按钮
        [flowButtonArr addObject:thumbnailsView.flowButton];
        [flowButtonArr addObject:thumbnailsView.oncePlayButton];
        [flowButtonArr addObject:thumbnailsView.cyclesPlayButton];
        
        
        int thumbWidth = thumbnailsView.frame.size.width;
        int thumbHeight = thumbnailsView.frame.size.height;
        UIView *viewInScroll = [[UIView alloc] initWithFrame:CGRectMake(5+(thumbWidth+16)*i, 10, thumbWidth+16, thumbHeight)];
//        viewInScroll.autoresizesSubviews = NO;
        viewInScroll.backgroundColor = [UIColor clearColor];
        [viewInScroll addSubview:thumbnailsView];
        [self.allThumbScrollView addSubview:viewInScroll];
    }
    self.allThumbScrollView.pagingEnabled = NO;
    self.allThumbScrollView.bounces = NO;
}


- (IBAction)expandAction:(id)sender {
    
    if([flowThumbArr count] <= 4)
    {
        return;
    }
   // [self.expandButton setBackgroundImage:[UIImage imageNamed:@"scan_openthumb.png"] forState:UIControlStateNormal];
    if(self.bottomSmallView.frame.size.height == 240.0)
    {
    [self.expandButton setBackgroundImage:[UIImage imageNamed:@"scan_closethumb.png"] forState:UIControlStateNormal];
        pageRightCounts = 0;
        pageLeftCounts = (self.flowCounts.text.integerValue-1)/8;
        pageAllLeft = pageLeftCounts;
        [self.bottomSmallView setFrame:Frame(self.bottomSmallView.frame.Xpos, self.bottomSmallView.frame.Ypos-200, self.bottomSmallView.frame.Width, self.bottomSmallView.frame.Height+200)];
        [self.pageTurnLeftButton setFrame:Frame(self.pageTurnLeftButton.frame.Xpos, self.pageTurnLeftButton.frame.Ypos+100, self.pageTurnLeftButton.frame.Width, self.pageTurnLeftButton.frame.Height)];
        [self.pageTurnRightButton setFrame:Frame(self.pageTurnRightButton.frame.Xpos, self.pageTurnRightButton.frame.Ypos+100, self.pageTurnRightButton.frame.Width, self.pageTurnRightButton.frame.Height)];
        [self.allThumbScrollView setFrame:Frame(self.allThumbScrollView.frame.Xpos, self.allThumbScrollView.frame.Ypos, self.allThumbScrollView.frame.Width, self.allThumbScrollView.frame.Height+200)];
        
        //重新排列流程缩略图:
        NSInteger   groupCounts = (self.flowCounts.text.integerValue-1)/8+1;//共多少组，每组8个
        self.allThumbScrollView.contentSize = CGSizeMake(groupCounts*self.allThumbScrollView.frame.size.width, self.allThumbScrollView.contentSize.height);
        BOOL    rowBool = NO;//是否是第2行
        NSInteger   j = 0;//每组中，每行的第几个
        NSInteger   k = 0;//第几组
        
        for(int i=0;i<[flowThumbArr count];i++)
        {
            ThumbView *thumbnailsView = [flowThumbArr objectAtIndex:i];
            if(i%8 == 0)
            {
                k++;
            }
            if(i%4 == 0 && i%8 != 0)
            {
                rowBool = YES;
                j = 0;
            }
            else if(i%4 == 0 && i%8 == 0)
            {
                rowBool = NO;
                j = 0;
            }
            j++;
            
            if(rowBool == NO)
            {
                UIView *viewInScroll = [[UIView alloc] initWithFrame:CGRectMake(5+(thumbnailsView.frame.size.width+16)*(j-1) + (k-1)*944, 10, thumbnailsView.frame.size.width+16, thumbnailsView.frame.size.height)];
                [viewInScroll addSubview:thumbnailsView];
                [self.allThumbScrollView addSubview:viewInScroll];
            }
            else
            {
                UIView *viewInScroll = [[UIView alloc] initWithFrame:CGRectMake(5+(thumbnailsView.frame.size.width+16)*(j-1) + (k-1)*944, 10+ thumbnailsView.frame.size.height+10, thumbnailsView.frame.size.width+16, thumbnailsView.frame.size.height)];
                [viewInScroll addSubview:thumbnailsView];
                [self.allThumbScrollView addSubview:viewInScroll];
            }
        }
        /***************************************************/
        
    }
    else
    {
    [self.expandButton setBackgroundImage:[UIImage imageNamed:@"scan_openthumb.png"] forState:UIControlStateNormal];
        pageRightCounts = 0;
        pageLeftCounts = (self.flowCounts.text.integerValue-1)/4;
        pageAllLeft = pageLeftCounts;
        
        [self.bottomSmallView setFrame:Frame(self.bottomSmallView.frame.Xpos, self.bottomSmallView.frame.Ypos+200, self.bottomSmallView.frame.Width, self.bottomSmallView.frame.Height-200)];
        [self.pageTurnLeftButton setFrame:Frame(self.pageTurnLeftButton.frame.Xpos, self.pageTurnLeftButton.frame.Ypos-100, self.pageTurnLeftButton.frame.Width, self.pageTurnLeftButton.frame.Height)];
        [self.pageTurnRightButton setFrame:Frame(self.pageTurnRightButton.frame.Xpos, self.pageTurnRightButton.frame.Ypos-100, self.pageTurnRightButton.frame.Width, self.pageTurnRightButton.frame.Height)];
        [self.allThumbScrollView setFrame:Frame(self.allThumbScrollView.frame.Xpos, self.allThumbScrollView.frame.Ypos, self.allThumbScrollView.frame.Width, self.allThumbScrollView.frame.Height-200)];
        
        //重新排列流程缩略图:
        self.allThumbScrollView.contentSize = CGSizeMake((944/4)*[flowThumbArr count], self.allThumbScrollView.contentSize.height);
        for(int i=0;i<[flowThumbArr count];i++)
        {
            ThumbView *thumbnailsView = [flowThumbArr objectAtIndex:i];
            UIView *viewInScroll = [[UIView alloc] initWithFrame:CGRectMake(5+(thumbnailsView.frame.size.width+16)*i, 10, thumbnailsView.frame.size.width+16, thumbnailsView.frame.size.height)];
            
            [viewInScroll addSubview:thumbnailsView];
            [self.allThumbScrollView addSubview:viewInScroll];
        }
        /***************************************************/
    }
}

- (IBAction)pageLeftAction:(id)sender {
    
    if(pageRightCounts>0)
    {
        pageLeftCounts++;
        pageRightCounts--;
        [self.allThumbScrollView setContentOffset:CGPointMake(self.allThumbScrollView.frame.origin.x+944*pageRightCounts-30, 0) animated:YES];
    }
}

- (IBAction)pageRightAction:(id)sender {
    
    if(pageLeftCounts>0)
    {
        pageLeftCounts--;
        pageRightCounts++;
        [self.allThumbScrollView setContentOffset:CGPointMake(self.allThumbScrollView.frame.origin.x+944*(pageAllLeft-pageLeftCounts)-30, 0) animated:YES];
    }

}


#pragma mark-
#pragma mark- ThumbViewDelegate

- (void)HandleFlowControlThumbDetailAction:(id)viewControler ButtonAction:(id)sender
{
    lastSelectedButtonID = 0;
    
    __block UIButton *senderButton = (UIButton *)sender;

    
    /*********************是否有流程正在播放isHaveFlowPlaying*************************/
    int i = 0;
    for(NSDictionary *dic in allFlowDataArr)
    {
        NSEnumerator    *enuObj = [dic objectEnumerator];
        for(NSDictionary *dic2 in enuObj)
        {
            //有流程正在播放
            if([[dic2 objectForKey:@"playFlag"] intValue] == 1)
            {
                isHaveFlowPlaying = YES;
                //是否当前流程正在播放
                if (i == (senderButton.tag-100)/3)
                {
                    currentFlowIsPlaying = YES;
                }
                else
                {
                    currentFlowIsPlaying = NO;
                }
                break;
            }
            else
            {
                isHaveFlowPlaying = NO;
                currentFlowIsPlaying = NO;
            }
        }
        if(isHaveFlowPlaying == YES)
        {
            break;
        }
        i++;
    }
    //播放 不是本流程
    if(isHaveFlowPlaying == YES && currentFlowIsPlaying == NO && ((senderButton.tag-100)%3 !=0))
    {
        [[MBProgressController sharedInstance] showTipsOnlyText:@"其他流程正在播放" AndDelay:1.5];
        return;
    }
    //点击进入流程节点界面
    else if (isHaveFlowPlaying == YES && currentFlowIsPlaying == YES && ((senderButton.tag-100)%3 ==0))
    {
        isNotNeedTurnNode = YES;
    }
    else if(isHaveFlowPlaying == YES && currentFlowIsPlaying == NO && ((senderButton.tag-100)%3 ==0))
    {
        isNotNeedTurnNode = NO;
    }
    else{
        isNotNeedTurnNode = YES;
    }
    /******************************************************************************/
    
    [flowButtonArr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx,BOOL *stop){
        UIButton *but = (UIButton *)obj;
        if(but.tag == senderButton.tag)
        {
            lastFlowNumber = (senderButton.tag-100)/3;
            if(senderButton.tag != lastSelectedButtonID)
            {
                if(lastSelectedButtonID == 0)
                {
                    lastSelectedButtonID = senderButton.tag;
                }
                else
                {
             //?????????????????
                    UIButton *lastButton = [flowButtonArr objectAtIndex:lastSelectedButtonID-100];
                    lastButton.selected = NO;
                    lastSelectedButtonID = senderButton.tag;
                }
            }
            /******************************点击进入*****************/
            if((senderButton.tag-100)%3 ==0) //点击进入
            {
                self.buttonIdInFlowBottom = senderButton.tag-100;
                //根据:senderButton.tag-100,来确定时第几个流程
                self.flowId = [[self.receiveData objectAtIndex:(senderButton.tag-100)/3] objectForKey:@"id"];
                self.flowName = [[self.receiveData objectAtIndex:(senderButton.tag-100)/3] objectForKey:@"name"];
                [self  NRLSMTGetFlowNodeForPad];
            }
            /******************************单次播放*****************/
            else if((senderButton.tag-100)%3 == 1)//单次播放
            {
                self.flowId = [[self.receiveData objectAtIndex:(senderButton.tag-100)/3] objectForKey:@"id"];
                

//                if (isCanActionOtherButton == 2) {
//                    return;
//                }
//                else
//                {
//                   isCanActionOtherButton = 2;//加锁：不允许点击其他播放按钮
//                }
                
//                if([allFlowDataArr[lastFlowNumber][self.flowId][@"pauseFlag"] intValue] !=1)
//                {
                    UIButton *nextButton =  [flowButtonArr objectAtIndex:idx+1];
                    nextButton.selected = NO;
                    [nextButton setTitle:@"循环播放" forState:UIControlStateNormal];
                    if (timer_playFlowScreen != nil) {
                    dispatch_source_cancel(timer_playFlowScreen);
                    }
               // }
                
                UIButton *onceButton = (UIButton *)sender;
                
                if(onceButton.selected == YES)
                {
                    allFlowDataArr[lastFlowNumber][self.flowId][@"pauseFlag"] = @(1);
                    if (timer_playFlowScreen != nil) {
                    dispatch_source_cancel(timer_playFlowScreen);
                    }
                    isPlayingFlag = NO;
                    isHaveFlowPlaying = NO;
                    allFlowDataArr[lastFlowNumber][self.flowId][@"playFlag"] = @(0);
                  //  isCanActionOtherButton = 0;//解锁：允许点击其他播放按钮
                    
                   // self.flowNoteIntervalTime.enabled = YES;
                    self.playingFlowLabel.text = @"无";
                    for(int i=0;i<[flowThumbArr count];i++)
                    {
                        ThumbView *thumbnails = flowThumbArr[i];
                        thumbnails.flowPlayShadow.hidden = YES;
                    }
                }
                else
                {
                    allFlowDataArr[lastFlowNumber][self.flowId][@"pauseFlag"] = @(0);
                    isPlayingFlag = YES;
                    
                    self.playingFlowLabel.text = [self.receiveData[lastFlowNumber] objectForKey:@"name"];
                    
                    for(int i=0;i<[flowThumbArr count];i++)
                    {
                        ThumbView *thumbnails = flowThumbArr[i];
                        if (i == lastFlowNumber)
                        {
                            thumbnails.flowPlayShadow.hidden = NO;
                        }
                        else
                        {
                            thumbnails.flowPlayShadow.hidden = YES;
                        }
                    }
                }
                if(onceButton.selected == NO)
                {
                    onceButton.selected = YES;
                }
                else
                {
                    onceButton.selected = NO;
                }
                
                [senderButton setTitle:@"暂停" forState:UIControlStateSelected];
                [senderButton setTitle:@"单次播放" forState:UIControlStateNormal];
                
                if(playButtonDic == nil)
                {
                    playButtonDic = [NSMutableDictionary dictionaryWithDictionary:@{@"button": onceButton,@"flag":@"once"}];
                }
                else
                {
                    [playButtonDic removeAllObjects];
                    playButtonDic = [NSMutableDictionary dictionaryWithDictionary:@{@"button": onceButton,@"flag":@"once"}];
                }
                
                
                NSLog(@"%ld:oncePlay",senderButton.tag-100);
                self.buttonIdInFlowBottom = senderButton.tag-100;
                
                self.flowName = [[self.receiveData objectAtIndex:(senderButton.tag-100)/3] objectForKey:@"name"];

                if([allFlowDataArr[lastFlowNumber][self.flowId][@"pauseFlag"] intValue] != 1)
                {
                    self.textFieldProme.hidden = NO;
                    [self  NRLSMTGetFlowNodeForPad];
                }
                else{
                self.textFieldProme.hidden = YES;
                }
            }
            else//循环播放
            {
                self.flowId = [[self.receiveData objectAtIndex:(senderButton.tag-100)/3] objectForKey:@"id"];
                
             //   if (isCanActionOtherButton == 2) {
//                    return;
//                }
//                else
//                {
//                    isCanActionOtherButton = 2;//加锁：不允许点击其他播放按钮
//                }
                
//                if([allFlowDataArr[lastFlowNumber][self.flowId][@"pauseFlag"] intValue] !=1)
//                {
                    //        return;
                    UIButton *lastButton =  [flowButtonArr objectAtIndex:idx-1];
                    lastButton.selected = NO;
                    [lastButton setTitle:@"单次播放" forState:UIControlStateNormal];
                    if (timer_playFlowScreen != nil) {
                    dispatch_source_cancel(timer_playFlowScreen);
                    }
                //}
                
                UIButton *cyclesButton = (UIButton *)sender;
                
                if(cyclesButton.selected == YES)
                {
                    allFlowDataArr[lastFlowNumber][self.flowId][@"pauseFlag"] = @(1);
                    if (timer_playFlowScreen != nil) {
                    dispatch_source_cancel(timer_playFlowScreen);
                    }
                    isPlayingFlag = NO;
                    isHaveFlowPlaying = NO;
                    allFlowDataArr[lastFlowNumber][self.flowId][@"playFlag"] = @(0);
                  //  isCanActionOtherButton = 0;//解锁：允许点击其他播放按钮
                    
                   // self.flowNoteIntervalTime.enabled = YES;
                    self.playingFlowLabel.text = @"无";
                    for(int i=0;i<[flowThumbArr count];i++)
                    {
                        ThumbView *thumbnails = flowThumbArr[i];
                        thumbnails.flowPlayShadow.hidden = YES;
                    }
                }
                else
                {
                    allFlowDataArr[lastFlowNumber][self.flowId][@"pauseFlag"] = @(0);
                    isPlayingFlag = YES;
                    
                    self.playingFlowLabel.text = [self.receiveData[lastFlowNumber] objectForKey:@"name"];
                    
                    for(int i=0;i<[flowThumbArr count];i++)
                    {
                        ThumbView *thumbnails = flowThumbArr[i];
                        if (i == lastFlowNumber)
                        {
                            thumbnails.flowPlayShadow.hidden = NO;
                        }
                        else
                        {
                            thumbnails.flowPlayShadow.hidden = YES;
                        }
                    }
                }
                if(cyclesButton.selected == NO)
                {
                    cyclesButton.selected = YES;
                }
                else
                {
                    cyclesButton.selected = NO;
                }
                
                [senderButton setTitle:@"暂停" forState:UIControlStateSelected];
                [senderButton setTitle:@"循环播放" forState:UIControlStateNormal];
                
                if(playButtonDic == nil)
                {
                    playButtonDic = [NSMutableDictionary dictionaryWithDictionary:@{@"button": cyclesButton,@"flag":@"cycles"}];
                }
                else
                {
                    [playButtonDic removeAllObjects];
                    playButtonDic = [NSMutableDictionary dictionaryWithDictionary:@{@"button": cyclesButton,@"flag":@"cycles"}];
                }
                
                NSLog(@"%d:cyclesPlay",senderButton.tag-100);
                self.buttonIdInFlowBottom = senderButton.tag-100;
                self.flowName = [[self.receiveData objectAtIndex:(senderButton.tag-100)/3] objectForKey:@"name"];

                if([allFlowDataArr[lastFlowNumber][self.flowId][@"pauseFlag"] intValue] != 1)
                {
                     self.textFieldProme.hidden = NO;
                    [self  NRLSMTGetFlowNodeForPad];
                }
                else{
                    self.textFieldProme.hidden = YES;
                }
            }
            *stop = YES;
        }
    }];
    
}

#pragma mark-
#pragma mark- FlowDetailViewDelegate
- (void)HandleNodePlayJumptoFlowPlay:(id)viewController ActionButton:(NSMutableDictionary *)playButtonDic
{
    NSMutableDictionary *playButDic = playButtonDic;
    
    UIButton *onceButton = (UIButton *)[flowButtonArr objectAtIndex:lastFlowNumber*3+1];
    UIButton *cyclesButton = (UIButton *)[flowButtonArr objectAtIndex:lastFlowNumber*3+2];
    
    UIButton    *tmpOnceButton = [playButtonDic objectForKey:@"oncebutton"];
    UIButton    *tmpCyclesButton = [playButtonDic objectForKey:@"cyclesbutton"];
    
    if ([tmpOnceButton.titleLabel.text isEqualToString:@"单次播放"]&&[tmpCyclesButton.titleLabel.text isEqualToString:@"循环播放"]) {
        self.textFieldProme.hidden = YES;
        if ([onceButton.titleLabel.text isEqualToString:@"暂停"]) {
            playButDic = [NSMutableDictionary dictionaryWithDictionary:@{@"button": [playButtonDic objectForKey:@"oncebutton"],@"flag":[playButtonDic objectForKey:@"onceflag"]}];
        }
        else if ([cyclesButton.titleLabel.text isEqualToString:@"暂停"])
        {
            playButDic = [NSMutableDictionary dictionaryWithDictionary:@{@"button": [playButtonDic objectForKey:@"cyclesbutton"],@"flag":[playButtonDic objectForKey:@"cyclesflag"]}];
        }
        else
        {
            allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"playFlag"] = @(0);
            allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"pauseFlag"] = @(0);//这边置为0是否有问题
            
            
            BOOL    isPlay = NO;
            
            for(NSDictionary *dic in allFlowDataArr)
            {
                NSEnumerator    *enuObj = [dic objectEnumerator];
                for(NSDictionary *dic2 in enuObj)
                {
                    if([[dic2 objectForKey:@"playFlag"] intValue] == 1)
                    {
                        isPlay = YES;
                        break;
                    }
                }
                if(isPlay == YES)
                {
                    break;
                }
            }

            if (isPlay == NO)
            {
                if(topImageViewInFlow != nil){
                    [topImageViewInFlow removeFromSuperview];
                }
                topImageViewInFlow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"flowControl.jpg"]];
                topImageViewInFlow.frame = CGRectMake(0, 0, Screen_Width-20, 430);
                [self.topViewInFlow addSubview:topImageViewInFlow];
            }
            return;
        }
    }
    
    if([[playButDic objectForKey:@"flag"] isEqualToString:@"once"])//单次
    {
        
        UIButton *but = (UIButton *)[playButDic objectForKey:@"button"];
        
        if(but.selected == YES)
        {
            isPlayingFlag = YES;
            onceButton.selected = NO;
        }
        else
        {
            allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"pauseFlag"] = @(1);
            isPlayingFlag = NO;
            onceButton.selected = NO;
        }
        
        if ([but.titleLabel.text isEqualToString:@"单次播放"]) {
            if(topImageViewInFlow != nil){
            [topImageViewInFlow removeFromSuperview];
            }
            topImageViewInFlow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"flowControl.jpg"]];
            topImageViewInFlow.frame = CGRectMake(0, 0, Screen_Width-20, 430);
            [self.topViewInFlow addSubview:topImageViewInFlow];
            if (timer_playFlowScreen != nil) {
                dispatch_source_cancel(timer_playFlowScreen);
            }
             self.textFieldProme.hidden = YES;
            for(int i=0;i<[flowThumbArr count];i++)
            {
                ThumbView *thumbnails = flowThumbArr[i];
                thumbnails.flowPlayShadow.hidden = YES;
            }
        }
        else{
             self.textFieldProme.hidden = NO;
            [self HandleFlowControlThumbDetailAction:self ButtonAction:onceButton];
        }
    }
    else//循环
    {
        UIButton *but = (UIButton *)[playButDic objectForKey:@"button"];
        
        if(but.selected == YES)
        {
            isPlayingFlag = YES;
            cyclesButton.selected = NO;
        }
        else
        {
            allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"pauseFlag"] = @(1);
            isPlayingFlag = NO;
            cyclesButton.selected = NO;
        }
        
        if ([but.titleLabel.text isEqualToString:@"循环播放"]) {
            if(topImageViewInFlow != nil){
            [topImageViewInFlow removeFromSuperview];
            }
            topImageViewInFlow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"flowControl.jpg"]];
            topImageViewInFlow.frame = CGRectMake(0, 0, Screen_Width-20, 430);
            [self.topViewInFlow addSubview:topImageViewInFlow];
            if (timer_playFlowScreen != nil) {
                dispatch_source_cancel(timer_playFlowScreen);
            }
             self.textFieldProme.hidden = YES;
            for(int i=0;i<[flowThumbArr count];i++)
            {
                ThumbView *thumbnails = flowThumbArr[i];
                thumbnails.flowPlayShadow.hidden = YES;
            }
        }
        else{
             self.textFieldProme.hidden = NO;
            [self HandleFlowControlThumbDetailAction:self ButtonAction:cyclesButton];
        }
    }
}

#pragma mark-
#pragma mark- AllNodesInScrollDelegate
- (void)HandleTapButtonForPlay:(id)viewController ButtonIndex:(NSNumber *)num
{
    allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"haveChooseNodeIndex"] = num;//存放当前的Node Index;
    [allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"topScreenView"] removeFromSuperview];
    NSString *flowNodeId = [[allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"receiveFlowNodeData"] objectAtIndex:StringFromId(allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"haveChooseNodeIndex"]).integerValue] objectForKey:@"id"];
    for(NSDictionary *dic in allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"nodeThumbnailArr"])
    {
        if([[dic allKeys] containsObject:flowNodeId])
        {
            allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"topScreenView"] = [[TopScreenView alloc] initWithFrame:CGRectMake(0, 0,[[dic objectForKey:flowNodeId] count]*100, 40)];
            break;
        }
    }
    NSMutableArray  *arr = [NSMutableArray array];
    NSMutableArray  *arrView = [NSMutableArray array];
    for(NSDictionary *dic in allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"nodeThumbnailArr"])
    {
        if([[dic allKeys] containsObject:flowNodeId])
        {
            for(NSDictionary *nodeDic in [dic objectForKey:flowNodeId])
            {
                [arr addObject:[nodeDic objectForKey:@"name"]];
                [arrView addObject:[nodeDic objectForKey:@"templateId"]];
            }
            break;
        }
    }
    TopScreenView   *topScreenView = allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"topScreenView"];
    topScreenView.delagateTopScreenForTap = self;
    topScreenView.nameArray = [NSArray arrayWithArray:arr];
    [self.topViewInFlow addSubview:topScreenView];
    [topScreenView initWithNameButtons];
    
    /********************每个节点的屏幕s****************************/
    [allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"rootScreenView"] removeFromSuperview];
    allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"rootScreenView"] = [[RootScreenView alloc] initWithFrame:CGRectMake(10, 114,(Screen_Width-20), 430-40)];
    RootScreenView  *rootScreenView = allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"rootScreenView"];
    rootScreenView.viewNameArray = [NSArray arrayWithArray:arrView];
    [self.view insertSubview:rootScreenView belowSubview:self.topViewInFlow];
    [rootScreenView initWithViews:[allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"screenNo"] intValue]];
}

#pragma mark-
#pragma mark- AllNodesInScrollDelegate
- (void)HandleNoteButtonLongPressPlay:(id)viewController ButtonID:(NSInteger)buttonIndex RECT:(CGRect) frame
{
    [self NRLSMTFlowNodeAutoPlay:[[allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"receiveFlowNodeData"] objectAtIndex:buttonIndex] objectForKey:@"id"] isTurn:@"true"];
}

- (void)HandleFlowControlPausePlay:(id)viewController ButtonIndex:(NSNumber *)num
{
    //此函数"循环播放”进不来:
    if(num.integerValue == 1)//单次
    {
        UIButton *onceButton = (UIButton *)[flowButtonArr objectAtIndex:isPlayingFlowNumber*3+1];
        
        allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"pauseFlag"] = @(0);
        allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"playFlag"] = @(0);
        
        onceButton.selected = NO;
        
        for(int i=0;i<[flowThumbArr count];i++)
        {
            ThumbView *thumbnails = flowThumbArr[i];
            thumbnails.flowPlayShadow.hidden = YES;
        }
        
        [onceButton setTitle:@"单次播放" forState:UIControlStateNormal];
        
//        [self HandleFlowControlThumbDetailAction:self ButtonAction:onceButton];
    }
//    else//循环
//    {
//        UIButton *cyclesButton = (UIButton *)[flowButtonArr objectAtIndex:isPlayingFlowNumber*3+2];
//        
//        allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"pauseFlag"] = @(2);
//        
//        cyclesButton.selected = YES;
//        [self HandleFlowControlThumbDetailAction:self ButtonAction:cyclesButton];
//    }
}


#pragma mark-
#pragma mark- TopScreenViewDelegate

- (void)HandleTapScreenButtonForFlowControl:(id)viewController ScreenIndex:(NSNumber *)num
{
    allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"screenNo"] = num;//lastFlowNumber,
    /********************每个节点的屏幕s****************************/
    [allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"rootScreenView"] removeFromSuperview];
    allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"rootScreenView"] = [[RootScreenView alloc] initWithFrame:CGRectMake(10, 114,(Screen_Width-20), 430-40)];
    RootScreenView  *rootScreenView = allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"rootScreenView"];
    NSMutableArray  *arrView = [NSMutableArray array];
    NSString *flowNodeId = [[allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"receiveFlowNodeData"] objectAtIndex:StringFromId(allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"haveChooseNodeIndex"]).integerValue] objectForKey:@"id"];
    for(NSDictionary *dic in allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"nodeThumbnailArr"])
    {
        if([[dic allKeys] containsObject:flowNodeId])
        {
            for(NSDictionary *nodeDic in [dic objectForKey:flowNodeId])
            {
                [arrView addObject:[nodeDic objectForKey:@"templateId"]];
            }
            break;
        }
    }
    
    rootScreenView.viewNameArray = [NSMutableArray arrayWithArray:arrView];
    [self.view insertSubview:rootScreenView belowSubview:self.topViewInFlow];
    [rootScreenView initWithViews:num.integerValue];
    /************************************************************/
}


#pragma mark-
#pragma mark- UITextFieldDelegate
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.flowNoteIntervalTime resignFirstResponder];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
{
    if(self.receiveData && [self.receiveData count]){
    CGRect r = self.bottomSmallView.frame;
    if(r.origin.y < 340.0){
        
    }
    else{
        
        r.origin.y = 340.0;
    }
    self.bottomSmallView.frame = r;
    
    return YES;
    }
    else
        return NO;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{

    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if(self.receiveData && [self.receiveData count]){
    CGRect r = self.bottomSmallView.frame;
    if(r.origin.y < 340.0){
        r.origin.y = 310.0;
    }
    else if(r.origin.y == 340.0){
        r.origin.y = 510.0;
    }
    self.bottomSmallView.frame = r;
    }
}
@end
